package com.zang.string;

/**
 * @author ZVerify
 * @since 2022/10/15 11:33
 **/
public class 替换空格 {

    public String replaceSpace(String s) {

        if (s == null || s.length()==0 ) return s;

        StringBuilder builder = new StringBuilder();
        for (char c : s.toCharArray()) {
            if (c == ' ') builder.append("   ");
        }
        int left = s.length()-1;
        s+=builder.toString();
        int right = s.length()-1;
        char[] chars = s.toCharArray();
        while (left>=0){
            if (chars[left] == ' '){
                chars[right--] = '0';
                chars[right--] = '2';
                chars[right] = '%';
            }else {
                chars[right] = chars[left];
            }
            left--;
            right--;
        }
        return new String(chars).substring(builder.length()/3);
    }
}
